//串口波特率2000000

/****************************************************************************
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 ****************************************************************************/

 /****************************************************************************
  * Included Files
  ****************************************************************************/

#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"

#include <lwip/tcpip.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h>
#include "sntp.h"

#include "bl_fw_api.h"
#include "wifi_mgmr_ext.h"
#include "wifi_mgmr.h"

#include "bflb_irq.h"
#include "bflb_uart.h"
#include "bflb_l1c.h"
#include "bflb_mtimer.h"

#include "bl616_glb.h"
#include "rfparam_adapter.h"

#include "board.h"


#define DBG_TAG "MAIN"
#include "log.h"


#include "lv_conf.h"
#include "lvgl.h"

#include "lv_port_disp.h"
#include "lv_port_indev.h"
#include "ui.h"
#include "user.h"

#include "lcd.h"
#include "portable.h"
#include "easyflash.h"
#include "bflb_mtd.h"
xQueueHandle queue;

#define WIFI_STACK_SIZE     (1536)
#define TASK_PRIORITY_FW    (16)

#if 1
static TaskHandle_t wifi_fw_task;
static int bl61x_get_heap_size(void);

/****************************************************************************
  * Functions
  ****************************************************************************/

int wifi_start_firmware_task(void)
{
    LOG_I("Starting wifi ...\r\n");

    /* enable wifi clock */

    GLB_PER_Clock_UnGate(GLB_AHB_CLOCK_IP_WIFI_PHY | GLB_AHB_CLOCK_IP_WIFI_MAC_PHY | GLB_AHB_CLOCK_IP_WIFI_PLATFORM);
    GLB_AHB_MCU_Software_Reset(GLB_AHB_MCU_SW_WIFI);

    /* set ble controller EM Size */

    GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB);

    if (0 != rfparam_init(0, NULL, 0)) {
        LOG_I("PHY RF init failed!\r\n");
        return 0;
    }

    LOG_I("PHY RF init success!\r\n");

    /* Enable wifi irq */

    extern void interrupt0_handler(void);
    bflb_irq_attach(WIFI_IRQn, (irq_callback)interrupt0_handler, NULL);
    bflb_irq_enable(WIFI_IRQn);

    xTaskCreate(wifi_main, (char*)"fw", WIFI_STACK_SIZE, NULL, TASK_PRIORITY_FW, &wifi_fw_task);

    return 0;
}
#endif
/* lvgl log cb */
void lv_log_print_g_cb(const char* buf)
{
    printf("[LVGL] %s", buf);
}

static TaskHandle_t lvgl_TaskHandle;
#define LVGL_STACK_SIZE 2048     //1024
#define LVGL_TASK_PRIORITY 15

void lvgl_task(void* param)
{

    while (1)
    {
        lv_task_handler();
        vTaskDelay(1);
    }
}

void io2_set(uint8_t value)
{
    struct bflb_device_s* gpio;
    gpio = bflb_device_get_by_name("gpio");
    bflb_gpio_init(gpio, GPIO_PIN_2, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
    if (value)
    {
        bflb_gpio_set(gpio, GPIO_PIN_2);
    }
    else
    {
        bflb_gpio_reset(gpio, GPIO_PIN_2);
    }

}

void lwip_sntp_init(void)
{
    sntp_setoperatingmode(SNTP_OPMODE_POLL);
    sntp_setservername(0, "ntp1.aliyun.com");
    sntp_init();
    printf("getservername:%s\r\n", sntp_getservername(0));
}

static void bl61x_show_heap_size_task(void* arg)
{
    while (1) {
        LOG_F("[%s:%d]heap_size-------> %d\r\n", DBG_TAG, __LINE__, bl61x_get_heap_size());
        vTaskDelay(3000/portTICK_PERIOD_MS);
    }
}

int main(void)
{

    board_init();

    tcpip_init(NULL, NULL);
    wifi_start_firmware_task();
    lwip_sntp_init();
    /*init easyflash*/
    bflb_mtd_init();
    easyflash_init();
    /* lvgl init */
    lv_log_register_print_cb(lv_log_print_g_cb);
    lv_init();
    lv_port_disp_init();
    lv_port_indev_init();

    ui_init();
    printf("lvgl success\r\n");
    
    user_queue_init();
    xTaskCreate(lvgl_task, (char*)"lvgl", LVGL_STACK_SIZE, NULL, LVGL_TASK_PRIORITY, &lvgl_TaskHandle);
    xTaskCreate(bl61x_show_heap_size_task, (char*)"heap", 1024, NULL, 2, NULL);

    vTaskStartScheduler();
    while (1) {
        vTaskDelay(5000/portTICK_RATE_MS);
    }
}
static int bl61x_get_heap_size(void)
{
    struct meminfo info1 = { 0 };
    struct meminfo info2 = { 0 };
    uint32_t total_free_size = 0;
    bflb_mem_usage(KMEM_HEAP, &info1);
    bflb_mem_usage(PMEM_HEAP, &info2);
    total_free_size = info1.free_size + info2.free_size;
    return total_free_size;
}